﻿
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading.Tasks;

using FreeSql;
using Kalman;
using Newtonsoft.Json.Linq;

namespace QuDao
{
    /// <summary>
    /// 订单项业务对象（处理数据表biz_order_item相关的业务逻辑）
    /// </summary>
    public sealed partial class OrderItemBO : SingletonBase<OrderItemBO>
    {
        readonly IFreeSql fsql;
        public OrderItemBO() : base()
        {
            fsql = ServiceLocator.Instance.GetRequiredService<IFreeSql>();
        }
        
        /// <summary>
        /// 获取订单项
        /// </summary>
        /// <param name="id">主键ID的值</param>
        /// <returns></returns>
        public T GetItem<T>(long id)
        {
        	if (id <= 0) return default;
            var item = fsql.Select<OrderItem>().Where(a => a.OrderItemID == id).ToOne<T>();
            return item;
        }
        
        public OrderItem GetItem(long id)
        {
        	return GetItem<OrderItem>(id);
        }
        public static long GetNewID()
        {
            var id = IDGenerator.Instance.GetIDByM1024() * 10 + RandomUtil.GetInteger(0, 9);
            return id;
        }

        /// <summary>
        /// 获取订单项数据列表
        /// </summary>
        /// <returns></returns>
        public List<OrderItemDto> GetList(long orderID)
        {
            var list = fsql.Select<OrderItem>().Where(a => a.OrderID == orderID).ToList<OrderItemDto>();
            return list;
        }

        /// <summary>
        /// 获取订单对应的项目列表字典（KEY：订单ID，VALUE：订单项目列表）
        /// </summary>
        /// <param name="orderIDs">订单IDs</param>
        /// <returns></returns>
        public Dictionary<long, List<OrderItemDto>> GetDic(long[] orderIDs)
        {
            var dic = new Dictionary<long, List<OrderItemDto>>();
            var list = fsql.Select<OrderItem>().Where(a => orderIDs.Contains(a.OrderID)).ToList<OrderItemDto>();

            foreach (var item in list)
            {
                if (dic.ContainsKey(item.OrderID))
                {
                    dic[item.OrderID].Add(item);
                }
                else
                {
                    dic.Add(item.OrderID, new List<OrderItemDto> { item });
                }
            }

            return dic;
        }

        /// <summary>
        /// 获取订单项数据列表
        /// </summary>
        /// <returns></returns>
        public List<T> GetList<T>(long[] ids)
        {
        	if (ids == null || ids.Length == 0) return new List<T>();
            var list = fsql.Select<OrderItem>().Where(a => ids.Contains(a.OrderItemID)).ToList<T>();
            return list;
        }
        
    }
}
